﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;

namespace Ru.NoteBoardPlus
{
    [ToolboxItemAttribute(false)]
    public class NoteBoardPlus : WebPart, INamingContainer
    {
        #region Private fields
        
        private int _displayItems = 3;
        private NoteBoardPlusControl _comment;

        #endregion

        [LocalizedWebDisplayName("wp_AttributeWebDisplayName")]
        [LocalizedCategory("wp_AttributeCategory")]
        [DefaultValue(3)]
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        public int DisplayItems
        {
            get { return _displayItems; }
            
            set { _displayItems = value; }
        }

        protected override void CreateChildControls()
        {
            _comment = new NoteBoardPlusControl();
            _comment.DisplayItems = _displayItems;
            this.Controls.Add(_comment);
        }

        #region Localozation of Web Part attributes

        /// <summary>
        /// Localized Category Web Part attribute
        /// </summary>
        /// <remarks>
        /// Walkthrough: Localizing a Web Part
        /// http://msdn.microsoft.com/en-us/library/gg491702.aspx
        /// </remarks>
        public sealed class LocalizedCategoryAttribute : CategoryAttribute
        {
            public LocalizedCategoryAttribute(string Category) : base(Category) { }

            // Override this method to return values from the webpart's resource file
            protected override string GetLocalizedString(string value)
            {
                return SPUtility.GetLocalizedString("$Resources:" + value, Constants.ResourceFile, (uint)CultureInfo.CurrentUICulture.LCID);
            }
        }

        /// <summary>
        /// Localized WebDisplayName Web Part attribute
        /// </summary>
        /// <remarks>
        /// Walkthrough: Localizing a Web Part
        /// http://msdn.microsoft.com/en-us/library/gg491702.aspx
        /// </remarks>
        public sealed class LocalizedWebDisplayNameAttribute : WebDisplayNameAttribute
        {
            bool _isLocalized = false;

            public LocalizedWebDisplayNameAttribute(string DisplayName) : base(DisplayName) { }

            public override string DisplayName
            {
                get
                {
                    if (!_isLocalized)
                    {
                        this.DisplayNameValue = SPUtility.GetLocalizedString("$Resources:" + base.DisplayName, Constants.ResourceFile, (uint)CultureInfo.CurrentUICulture.LCID);
                        _isLocalized = true;
                    }
                    return base.DisplayName;
                }
            }
        }

        #endregion
    }
}
